import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import javax.swing.JFormattedTextField;
import java.awt.Insets;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


@SuppressWarnings("serial")
public class CustomizationPanel
extends JPanel
{
    private Clock clock;
    
    public CustomizationPanel(final Clock clock)
    {
        GridBagLayout gridBagLayout = new GridBagLayout();
        gridBagLayout.columnWidths = new int[] { 0, 198, 0 };
        gridBagLayout.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        gridBagLayout.columnWeights = new double[] { 0.0, 1.0, Double.MIN_VALUE };
        gridBagLayout.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
        setLayout(gridBagLayout);
        
        JLabel lblSpirals = new JLabel("spir");
        GridBagConstraints gbc_lblSpirals = new GridBagConstraints();
        gbc_lblSpirals.insets = new Insets(0, 0, 5, 5);
        gbc_lblSpirals.anchor = GridBagConstraints.EAST;
        gbc_lblSpirals.gridx = 0;
        gbc_lblSpirals.gridy = 0;
        add(lblSpirals, gbc_lblSpirals);
        
        final JFormattedTextField spiralsFormattedTextField = new JFormattedTextField();
        spiralsFormattedTextField.setText(""+clock.getSpirals());
        GridBagConstraints gbc_formattedTextField = new GridBagConstraints();
        gbc_formattedTextField.insets = new Insets(0, 0, 5, 0);
        gbc_formattedTextField.fill = GridBagConstraints.HORIZONTAL;
        gbc_formattedTextField.gridx = 1;
        gbc_formattedTextField.gridy = 0;
        add(spiralsFormattedTextField, gbc_formattedTextField);
        
        JLabel lblRadMin = new JLabel("rad min");
        GridBagConstraints gbc_lblRadMin = new GridBagConstraints();
        gbc_lblRadMin.anchor = GridBagConstraints.EAST;
        gbc_lblRadMin.insets = new Insets(0, 0, 5, 5);
        gbc_lblRadMin.gridx = 0;
        gbc_lblRadMin.gridy = 1;
        add(lblRadMin, gbc_lblRadMin);
        
        final JFormattedTextField minFormattedTextField = new JFormattedTextField();
        minFormattedTextField.setText(""+clock.getRadianMinuteStep());
        GridBagConstraints gbc_formattedTextField1 = new GridBagConstraints();
        gbc_formattedTextField1.insets = new Insets(0, 0, 5, 0);
        gbc_formattedTextField1.fill = GridBagConstraints.HORIZONTAL;
        gbc_formattedTextField1.gridx = 1;
        gbc_formattedTextField1.gridy = 1;
        add(minFormattedTextField, gbc_formattedTextField1);
        
        JLabel lblRadH = new JLabel("rad h");
        GridBagConstraints gbc_lblRadH = new GridBagConstraints();
        gbc_lblRadH.anchor = GridBagConstraints.EAST;
        gbc_lblRadH.insets = new Insets(0, 0, 5, 5);
        gbc_lblRadH.gridx = 0;
        gbc_lblRadH.gridy = 2;
        add(lblRadH, gbc_lblRadH);
        
        final JFormattedTextField hourFormattedTextField = new JFormattedTextField();
        hourFormattedTextField.setText(""+clock.getRadianHourStep());
        GridBagConstraints gbc_formattedTextField_1 = new GridBagConstraints();
        gbc_formattedTextField_1.insets = new Insets(0, 0, 5, 0);
        gbc_formattedTextField_1.fill = GridBagConstraints.HORIZONTAL;
        gbc_formattedTextField_1.gridx = 1;
        gbc_formattedTextField_1.gridy = 2;
        add(hourFormattedTextField, gbc_formattedTextField_1);
        
        JButton btnApply = new JButton("Apply");
        btnApply.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                SwingUtilities.invokeLater(new Runnable()
                {
                    
                    @Override
                    public void run()
                    {
                        try
                        {
                            clock.setSpirals(Double.parseDouble(spiralsFormattedTextField.getText()));
                            clock.setRadianHourStep(Double.parseDouble(hourFormattedTextField.getText()));
                            clock.setRadianMinuteStep(Double.parseDouble(minFormattedTextField.getText()));
                        }
                        catch (Exception e1)
                        {
                            JOptionPane.showMessageDialog(null, "Parameter error", "ERRRRRRRRORRRRRRRRRrrrrg keuf keuf", JOptionPane.ERROR_MESSAGE);
                        }
                    }
                });
            }
        });
        GridBagConstraints gbc_btnApply = new GridBagConstraints();
        gbc_btnApply.fill = GridBagConstraints.HORIZONTAL;
        gbc_btnApply.gridwidth = 2;
        gbc_btnApply.gridx = 0;
        gbc_btnApply.gridy = 9;
        add(btnApply, gbc_btnApply);
    }
}
